package com.masterclient.app.modules.goods.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.commonbean.product.CommodityOversea;
import com.eoner.commonbean.product.ProductMessage;
import com.eoner.commonbean.product.ProductTag;
import com.masterclient.app.R;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;


public class CommodityRecyclerAdapter extends BaseQuickAdapter<ProductMessage, BaseViewHolder> {


    private int width;
    private String[] strings;
    private boolean isShareEarn;
    /*
    **source_page, source_module, source_element
     */
    public void setStrings(String... strings) {
        this.strings = strings;
    }

    public CommodityRecyclerAdapter() {
        super(R.layout.item_footer_good);
        this.strings = new String[]{};
        width = (int)((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(41))/2f);
        isShareEarn = false;
    }

    public void setShareEarn(boolean shareEarn) {
        isShareEarn = shareEarn;
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductMessage item) {
        //渲染价格
        renderAllPrice(helper,item);

        TextView tvFavoriteNum =  helper.getView(R.id.tv_favorite_num);
        tvFavoriteNum.setText(item.getFavorite_num()+"人喜欢");
        tvFavoriteNum.setVisibility(View.GONE);

        ImageView imageView =  helper.getView(R.id.iv_goods);
//        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
//        layoutParams.height = item.getHomeWidth();
//        layoutParams.width = item.getHomeWidth();
        ViewTransformUtil.glideImageView(mContext,item.getImage(),imageView,new RoundedCornersTransformation(0, 0),
                R.mipmap.defaults,width,width);

        List<ProductTag> tags =  item.getTags();
        String new_member_tag = "";//新人标签
        String present_tag = "";//赠品标签
        String activity_mj_tag = "";//满减标签
        String deduction_rate_tag = "";//金币抵扣
        if (tags != null && tags.size()>0) {
            for (ProductTag tag : tags) {
                if ("2".equals(tag.getType())) {
                    new_member_tag = tag.getName();
                } else if ("3".equals(tag.getType())) {
                    activity_mj_tag = tag.getName();
                } else if ("4".equals(tag.getType())) {
                    present_tag = tag.getName();
                } else if ("5".equals(tag.getType())) {
                    deduction_rate_tag = tag.getName();
                }
            }
        }

        String country = "",country_logo = "",country_tag = "";
        CommodityOversea oversea = item.getOversea();
        if(oversea != null){
            country = oversea.getCountry_name();
            country_logo = oversea.getCountry_logo();
            country_tag = oversea.getTag();
        }

        helper.setText(R.id.tv_sub_title,item.getTitle());

        ImageView ivTagNew = helper.getView(R.id.iv_tag_new);

        if("1".equals(item.isIs_new())){
            ivTagNew.setVisibility(View.VISIBLE);
        }else {
            ivTagNew.setVisibility(View.GONE);
        }

//        renderActivityImage(helper,item);

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewCommodityDetailActivity.class);
                intent.putExtra("id",item.getProduct_id());
                intent.putExtra("source_page", strings.length>0?strings[0]:"");
                intent.putExtra("source_module",strings.length>1?strings[1]:"");
                intent.putExtra("source_element",strings.length>2?strings[2]:"");
                intent.putExtra("key_word", strings.length>3?strings[3]:"");
                intent.putExtra("key_word_type", strings.length>4?strings[4]:"");
                intent.putExtra("source_position",helper.getAdapterPosition()-getHeaderLayoutCount()+"");
                mContext.startActivity(intent);
            }
        });
    }


    private void renderAllPrice(BaseViewHolder helper, ProductMessage item) {
        TextView tvPrice = helper.getView(R.id.price);
        TextView tvOfficialPrice = helper.getView(R.id.tv_official_price);

        Utils.set_price_style2(tvPrice,"¥ " + item.getPrice());
        Utils.setDin(tvPrice,mContext);
        tvOfficialPrice.setText("¥" +item.getLine_price());
        tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        tvOfficialPrice.setVisibility(View.VISIBLE);
    }


}
